package com.fzz.controller;

import com.fzz.dao.ViewMapping;
import com.fzz.domain.View;
import com.fzz.service.ViewService;
import com.fzz.serviceImpl.ViewServiceImpl;
import com.fzz.tools.GetImgUrl;
import com.fzz.tools.GetName;
import com.fzz.tools.GetViewUrl;
import com.fzz.tools.Utils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class TestController {

    @Autowired
    private ViewService viewService=new ViewServiceImpl();
    @RequestMapping("/index")
    public String toIndex(Model model)
    {
       /* List<View> viewList = viewService.loadView();
        model.addAttribute("viewList",viewList);
        for (View view : viewList) {
            System.out.println(view);
        }*/

        List<View> list = viewService.loadView();
        model.addAttribute("viewList",list);
        for (View view : list) {
            System.out.println(view);
        }
        return "index";
    }

    @RequestMapping("/load")
    public String load(Model model) throws IOException {
        Utils utils=new Utils();
        List<String> list1=new ArrayList<String>();
        List<List<String>> list2=new ArrayList<List<String>>();
        List<List<String>> list3=new ArrayList<List<String>>();
        list1=utils.getViewUrl("http://www.imomoe.ai/list/1_7.html");
        List<String> collect = list1.stream().distinct().collect(Collectors.toList());
        for (String s : collect) {
            String url="http://www.imomoe.ai"+s;
            list2.add(utils.getPlayUrl(url));
        }

        for (List<String> list : list2) {
            for (String s : list) {
                String url="http://www.imomoe.ai"+s;
                list3.add(utils.getVideoUrl(url));
            }
        }

        for (String s : collect) {
            System.out.println(s);
        }
        System.out.println("++++++++++++++++++++++++++++++++++++");
        for (List<String> list : list2) {
            for (String s : list) {
                System.out.println(s);
            }
        }
        System.out.println("++++++++++++++++++++++++++++++++++++");
        for (List<String> list : list3) {
            for (String s : list) {
                System.out.println(s);
            }
        }
        List<View> list=new ArrayList<View>();
        View view = new View();
        for (List<String> viewLists : list3) {
            if(viewLists.size()>0)
            {
                for (int i=0;i<viewLists.size();i++) {
                    if(i==0)
                    {
                        view.setViewUrl(viewLists.get(i));
                    }if(i==1)
                    {
                        view.setImgUrl(viewLists.get(i));
                    }
                    if(i==2)
                    {
                        view.setViewName(viewLists.get(i));
                    }
                }
                viewService.addView(view);
            }

        }

        list=viewService.loadView();
        model.addAttribute("viewList",list);
        return "index";
    }


/*    @GetMapping(value = "/")
    private String index(HttpServletRequest request,Model model)
    {
        return this.index(request,model,1,25);
    }*/

    @GetMapping(value = "/")
    private String index(HttpServletRequest request,Model model,@RequestParam(value = "p",defaultValue = "1") int page, @RequestParam(value = "count",defaultValue = "25") int count) {

        PageInfo<View> views=viewService.selectViewWithPage(model,page,count);
        model.addAttribute("viewList",views);
        System.out.println(views.toString());
        return "index";
    }

}
